<?php
namespace BP\Sys;
require_once("BP/Tools/StringHelper.php");
class FrmEvents extends EntitiesOID {
	public function DoEventNode_String_Entity ($dotype, $en) // [String dotype, Entity en]
	{
			/* match: String_Entity_String */
		return $this->DoEventNode_String_Entity_String($dotype, $en, NULL);
	}
	public function DoEventNode_String_Entity_String ($dotype, $en, $atPara) // [String dotype, Entity en, String atPara]
	{
		if (($this->size() == 0))
		{
			return NULL;
		}
		$val = $this->_DoEventNode($dotype, $en, $atPara);
		if (($val . NULL))
		{
			$val = $val->trim();
		}
		if ($DataType->IsNullOrEmpty($val))
		{
			return "";
		}
		else
		{
			return $val;
		}
	}
	protected function _DoEventNode ($dotype, $en, $atPara) // [String dotype, Entity en, String atPara]
	{
		if (($this->size() == 0))
		{
			return NULL;
		}
		$tempVar = $this->GetEntityByKey(FrmEventAttr::$FK_Event, $dotype);
		$nev = ( ($tempVar instanceof FrmEvent) ? $tempVar : NULL );
		if ((($nev == NULL) || ($nev->getHisDoType() == EventDoType::$Disable)))
		{
			return NULL;
		}
		if (($nev->getHisDoType() == EventDoType::$BuessUnit))
		{
			$enBuesss = Glo->GetBuessUnitEntityByEnName($nev->getDoDoc());
			$enBuesss->WorkID = $Long->parseLong($en->getPKVal());
			return $enBuesss->DoIt();
		}
		$doc = $nev->getDoDoc()->trim();
		if ((((($doc . NULL) . $doc->equals(""))) . ($nev->getHisDoType() != EventDoType::$SpecClass)))
		{
			return NULL;
		}
		$attrs = $en->getEnMap()->getAttrs();
		$MsgOK = "";
		$MsgErr = "";
		foreach ($attrs as $attr) 		{
			if (($doc->contains(("@" . $attr->getKey())) ==  FALSE ))
			{
				break;
			}
			if (((($attr->getMyDataType() == $DataType->AppString) || ($attr->getMyDataType() == $DataType->AppDateTime)) || ($attr->getMyDataType() == $DataType->AppDate)))
			{
				$doc = $doc->replace(("@" . $attr->getKey()), (("'" . $en->GetValStrByKey($attr->getKey())) . "'"));
			}
			else
			{
				$doc = $doc->replace(("@" . $attr->getKey()), $en->GetValStrByKey($attr->getKey()));
			}
		}
		$doc = $doc->replace("~", "'");
		$doc = $doc->replace("@WebUser.getNo()", $WebUser->getNo());
		$doc = $doc->replace("@WebUser.getName()", $WebUser->getName());
		$doc = $doc->replace("@WebUser.getFK_Dept()", $WebUser->getFK_Dept());
		$doc = $doc->replace("@FK_Node", $nev->getFK_MapData()->replace("ND", ""));
		$doc = $doc->replace("@FK_MapData", $nev->getFK_MapData());
		$doc = $doc->replace("@WorkID", $en->GetValStrByKey("OID", "@WorkID"));
		$doc = $doc->replace("@SDKFromServHost", SystemConfig::getAppSettings()->get("SDKFromServHost"));
		if (($doc->contains("@") ==  TRUE ))
		{
			$url = Glo::getRequest()->getRemoteAddr();
			if (($url->indexOf('?') != -1))
			{
				$url = $url->substring($url->indexOf('?'));
			}
			$paras = $url->split("[&]", -1);
			foreach ($paras as $s) 			{
				$mys = $s->split("[=]", -1);
				if (($doc->contains(("@" . $mys[0])) ==  FALSE ))
				{
					continue;
				}
				$doc = $doc->replace(("@" . $mys[0]), $mys[1]);
			}
		}
		if (($nev->getHisDoType() == EventDoType::$URLOfSelf))
		{
			if (($doc->contains("?") ==  FALSE ))
				$doc += "?1=2";
			$doc += ("&UserNo=" . $WebUser->getNo());
			$doc += ("&SID=" . $WebUser->getSID());
			$doc += ("&FK_Dept=" . $WebUser->getFK_Dept());
			$doc += ("&OID=" . $en->getPKVal());
			if ((SystemConfig::getIsBSsystem() && (Glo->getRequest() != NULL)))
			{
				$url = Glo->getRequest()->getRemoteAddr();
				if (($url->indexOf('?') != -1))
				{
					$url = $url->substring($url->indexOf('?'));
				}
				$paras = $url->split("[&]", -1);
				foreach ($paras as $s) 				{
					$mys = $s->split("[=]", -1);
					if ($doc->contains(($mys[0] . "=")))
					{
						continue;
					}
					$doc += ("&" . $s);
				}
				$doc = $doc->replace("&?", "&");
			}
			$doc += ((((((("&EntityName=" . $en) . "&EntityPK=") . $en->getPK()) . "&EntityPKVal=") . $en->getPKVal()) . "&FK_Event=") . $nev->getMyPK());
		}
		if ((($atPara . NULL) . ($doc->contains("@") ==  TRUE )))
		{
			$ap = new AtPara($atPara);
			foreach ($ap->getHisHT()->keySet() as $s) 			{
				$doc = $doc->replace(("@" . $s), $ap->GetValStrByKey($s));
			}
		}
		if (FrmEventList::$FrmLoadBefore->equals($dotype))
		{
			$en->Retrieve();
		}
		switch ($nev->getHisDoType()) {
			case $SP:
			case $SQL:
				try 
				{
					DBAccess->RunSQLs($doc);
					return $nev->MsgOK($en);
				}
				catch (\RuntimeException $ex)
				{
					throw new \RuntimeException((($nev->MsgError($en) . " Error:") . $ex->getMessage()));
				}
			case $URLOfSelf:
				$tempVar2 = new String($doc);
				$myURL = (( (($tempVar2 instanceof String)) ? $tempVar2 : NULL ));
				if (($myURL->contains("http") ==  FALSE ))
				{
					if (SystemConfig::getIsBSsystem())
					else
					{
						$cfgBaseUrl = $BP->WF->Glo->getHostURL();
						if (is_null($cfgBaseUrl) || $cfgBaseUrl == '')
						{
							$err = "调用url失败:没有在web.config中配置BaseUrl,导致url事件不能被执�?.";
							$Log->DefaultLogWriteLineError($err);
							throw new \RuntimeException($err);
						}
						$myURL = ($cfgBaseUrl . $myURL);
					}
				}
				$myURL = $myURL->replace("@SDKFromServHost", SystemConfig::getAppSettings()->get("SDKFromServHost"));
				if ((($myURL->contains("&FID=") ==  FALSE ) && ($en->getRow()->containsKey("FID") ==  TRUE )))
				{
					$str = $en->getRow()->get("FID");
					$myURL = (($myURL . "&FID=") . $str);
				}
				if ((($myURL->contains("&FK_Flow=") ==  FALSE ) && ($en->getRow()->containsKey("FK_Flow") ==  TRUE )))
				{
					$str = $en->getRow()->get("FID");
					$myURL = (($myURL . "&FK_Flow=") . $str);
				}
				if ((($myURL->contains("&WorkID=") ==  FALSE ) && ($en->getRow()->containsKey("WorkID") ==  TRUE )))
				{
					$str = $en->getRow()->get("WorkID");
					$myURL = (($myURL . "&WorkID=") . $str);
				}
				try 
				{
					$text = $DataType->ReadURLContext($myURL, 600000);
					if (($text . NULL))
					{
						throw new \RuntimeException((("@流程设计错误，执行的url错误:" . $myURL) . ", 返回为null, 请检查url设置是否正确。提示：您可以copy出来此url放到浏览器里看看是否被正确执行�??"));
					}
					if (((($text . NULL) . ($text->length() > 7)) . $text->substring(0, 7)->toLowerCase()->contains("err")))
					{
						throw new \RuntimeException($text);
					}
					if ((($text . NULL) . $text->trim()->equals("")))
					{
						return NULL;
					}
					return $text;
				}
				catch (\RuntimeException $ex)
				{
					throw new \RuntimeException(((("@" . $nev->MsgError($en)) . " Error:") . $ex->getMessage()));
				}
			case EventBase:
				$tempVar3 = $doc;
				$evName = ( ($tempVar3 instanceof String) ? $tempVar3 : NULL );
				$ev = NULL;
				try 
				{
					$ev = $BP->En->ClassFactory->GetEventBase($evName);
				}
				catch (\RuntimeException $ex)
				{
					throw new \RuntimeException((("@事件名称:" . $evName) . "拼写错误，或者系统不存在。说明：事件�?在的类库必须是以BP.�?头，并且类库的BP.xxx.dll�?"));
				}
				try 
				{
					$r = $en->getRow();
					try 
					{
						$r->put("FK_MapData", $en->getClassID());
					}
					catch (\Exception $e)
					{
						$r->put("FK_MapData", $en->getClassID());
					}
					try 
					{
						$r->put("EventType", $nev->getFK_Event());
					}
					catch (\Exception $e2)
					{
						$r->put("EventType", $nev->getFK_Event());
					}
					if (($atPara . NULL))
					{
						$ap = new AtPara($atPara);
						foreach ($ap->getHisHT()->keySet() as $s) 						{
							try 
							{
								$r->put($s, $ap->GetValStrByKey($s));
							}
							catch (\Exception $e3)
							{
								$r->put($s, $ap->GetValStrByKey($s));
							}
						}
					}
					if ((SystemConfig::getIsBSsystem() ==  TRUE ))
					{
						$params = Glo->getRequest()->getParameterNames();
						while ($params->hasMoreElements()) 
						{
							$key = $params->nextElement();
							$val = Glo->getRequest()->getParameter($key);
							try 
							{
								$r->put($key, $val);
							}
							catch (\Exception $e4)
							{
								$r->put($key, $val);
							}
						}
					}
					$ev->setSysPara($r);
					$ev->HisEn = $en;
					$ev->Do();
					return $ev->SucessInfo;
				}
				catch (\RuntimeException $ex)
				{
					throw new \RuntimeException(((("@执行事件(" . $ev->getTitle()) . ")期间出现错误:") . $ex->getMessage()));
				}
			case $WSOfSelf:
				$strs = $doc->split("[@]", -1);
				$url = "";
				$method = "";
				$paras = new Hashtable();
				foreach ($strs as $str) 				{
					if (($str->contains("=") && $str->contains("Url")))
					{
						$url = $str->split("[=]", -1)[2];
						continue;
					}
					if (($str->contains("=") && $str->contains("Method")))
					{
						$method = $str->split("[=]", -1)[2];
						continue;
					}
					$paraKeys = $str->split("[,]", -1);
					if ($paraKeys[3]->equals("Int"))
					{
						$paras->put($paraKeys[0], $Integer->parseInt($paraKeys[1]));
					}
					if ($paraKeys[3]->equals("String"))
					{
						$paras->put($paraKeys[0], $paraKeys[1]);
					}
					if ($paraKeys[3]->equals("Float"))
					{
						$paras->put($paraKeys[0], $Float->parseFloat($paraKeys[1]));
					}
					if ($paraKeys[3]->equals("Double"))
					{
						$paras->put($paraKeys[0], $Double->parseDouble($paraKeys[1]));
					}
				}
				return NULL;
			case $SpecClass:
			default:
				throw new \RuntimeException(("@no such way." . $nev->getHisDoType()));
		}
	}
	public static function constructor__ () 
	{
		$me = new self();
		return $me;
	}
	public static function constructor__String ($fk_MapData) // [String fk_MapData]
	{
		$me = new self();
		$qo = new QueryObject($me);
		$qo->AddWhere(FrmEventAttr::$FK_MapData, $fk_MapData);
		$qo->DoQuery();
		return $me;
	}
	public static function constructor__I ($nodeID) // [int nodeID]
	{
		$me = new self();
		$qo = new QueryObject($me);
		$qo->AddWhere(FrmEventAttr::$FK_Node, $nodeID);
		$qo->DoQuery();
		return $me;
	}
	public function getNewEntity () 
	{
		return FrmEvent::constructor__();
	}
	public function ToJavaList () 
	{
		return $this;
	}
	public function Tolist () 
	{
		$list = [];// 
		for ($i = 0; ($i < $this->size()); ++$i) 
		{
			$list->add($this->get($i));
		}
		return $list;
	}
}
?>
